package org.morningangel.servlet.android;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.morningangel.bean.User;
import org.morningangel.dao.UserDao;
import org.morningangel.dao.impl.UserDaoImpl;

@WebServlet(name = "AndroidBindQqServlet", urlPatterns = "/android/bindQq")
public class BindQqServlet extends HttpServlet {

	private static final long serialVersionUID = -9153005224962872087L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	    Integer userId  = Integer.parseInt(request.getParameter("userId"));
		String qq = request.getParameter("qq");
		UserDao userDao = new UserDaoImpl();
		User user = userDao.getById(userId);
		Integer result = 0;
		if (user.getQq() != null && user.getQq().equals(qq)) {
			result = 0;
		} else {
			user.setQq(qq);
			userDao.update(user);
			result = 1;
		}
		
		PrintWriter out = response.getWriter();
		out.print("{\"isSuccess\":\"" + result + "\"}");
		out.flush();
		out.close();
	}

}
